﻿using System;
using Kaleida.ServiceMonitor.Operations.ResponseHandlers;
using NUnit.Framework;

namespace Kaleida.UnitTests.Operations.ResponseHandlers
{
    [TestFixture]
    public class MustContainTests
    {
        [Test]
        public void TestStringArgument()
        {
            Assert.AreEqual("Found string within document of length 3 characters", new MustContain("").ProcessResponse("abc"));
            Assert.AreEqual("Found string within document of length 1 character", new MustContain("1").ProcessResponse("1"));
            Assert.AreEqual("Found string within document of length 3 characters", new MustContain("123").ProcessResponse("123"));
            Assert.AreEqual("Found string within document of length 8 characters", new MustContain("-123").ProcessResponse(" -123456"));
            Assert.AreEqual("Found string within document of length 20 characters", new MustContain(@"line1\r\nline2").ProcessResponse(" line1\r\nline2\r\nline3"));
            Assert.AreEqual("Found string within document of length 6 characters", new MustContain("abcdef").ProcessResponse("abcdef"));

            var exception = Assert.Throws<InvalidOperationException>(() => new MustContain("abc").ProcessResponse(" Abcd"));
            Assert.AreEqual("Expected response to contain 'abc' but text was not found", exception.Message);
        }

        [Test]
        public void TestStringContainerArgument()
        {
            Assert.AreEqual("Found string within document of length 3 characters", new MustContain("").ProcessResponse(new MockStringContainer("abc")));
            Assert.AreEqual("Found string within document of length 1 character", new MustContain("1").ProcessResponse(new MockStringContainer("1")));
            Assert.AreEqual("Found string within document of length 3 characters", new MustContain("123").ProcessResponse(new MockStringContainer("123")), "Content is used in preference to ToString()");
            Assert.AreEqual("Found string within document of length 8 characters", new MustContain("-123").ProcessResponse(new MockStringContainer(" -123456")), "Content is used in preference to ToString()");
            Assert.AreEqual("Found string within document of length 20 characters", new MustContain(@"line1\r\nline2").ProcessResponse(new MockStringContainer(" line1\r\nline2\r\nline3")));
            Assert.AreEqual("Found string within document of length 6 characters", new MustContain("abc").ProcessResponse(new MockStringContainer("abcdef")), "Content is used in preference to ToString()");

            var exception = Assert.Throws<InvalidOperationException>(() => new MustContain("abc").ProcessResponse(new MockStringContainer(" Abcd")));
            Assert.AreEqual("Expected response to contain 'abc' but text was not found", exception.Message);
        }
    }
}
